<div class="content-wrapper">
    <?php
    $form = $this->form;
    $form->prepare();
    if(isset($this->couponInfo))$form->get('getCouponType')->setAttributes(['disabled' => true]);
    ?>
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dbshop-menu-top">
            <a href="<?php echo $this->url('operation-coupon'); ?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回优惠券列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('保存优惠券'); ?></button>
            <?php echo $this->HelpUrl('coupon'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab"><?php echo $this->translate('基本信息'); ?></a></li>
                        <li><a href="#tab_2" data-toggle="tab"><?php echo $this->translate('优惠规则'); ?></a></li>
                        <li><a href="#tab_3" data-toggle="tab"><?php echo $this->translate('获取规则'); ?></a></li>
                    </ul>
                    <div class="tab-content">
                        <?php echo $this->partial('layout/messages'); ?>
                        <div class="tab-pane active" id="tab_1">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('优惠名称'); ?><span class="must_add_value">*</span></label>
                                    <div class="col-sm-3">
                                        <?php echo $this->formElement($form->get('couponTitle')); ?>
                                        <?php echo $this->formElementErrors($form->get('couponTitle'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('优惠描述'); ?><span class="must_add_value">*</span></label>
                                    <div class="col-sm-5">
                                        <?php echo $this->formElement($form->get('couponInfo')); ?>
                                        <?php echo $this->formElementErrors($form->get('couponInfo'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('开始时间'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <?php echo $this->formElement($form->get('couponStartTime')); ?>
                                        </div>
                                        <?php echo $this->formElementErrors($form->get('couponStartTime'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('结束时间'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <?php echo $this->formElement($form->get('couponEndTime')); ?>
                                        </div>
                                        <?php echo $this->formElementErrors($form->get('couponEndTime'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('状态'); ?></label>
                                    <div class="col-sm-4">
                                        <div class="checkbox">
                                            <label>
                                                <?php echo $this->formElement($form->get('couponState')); ?><?php echo $this->translate('启用'); ?>
                                                <?php echo $this->formElementErrors($form->get('couponState'), ['class'=>'error-message']); ?>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="tab-pane" id="tab_2">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('购物金额满'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <span class="input-group-addon"><?php echo $this->currencySymbol; ?></span>
                                            <?php echo $this->formElement($form->get('shoppingAmount')); ?>
                                        </div>
                                        <?php echo $this->formElementErrors($form->get('shoppingAmount'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('优惠(立减)'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <span class="input-group-addon"><?php echo $this->currencySymbol; ?></span>
                                            <?php echo $this->formElement($form->get('shoppingDiscount')); ?>
                                        </div>
                                        <?php echo $this->formElementErrors($form->get('shoppingDiscount'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="tab-pane" id="tab_3">
                            <div class="box-body">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('开始时间'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <?php echo $this->formElement($form->get('getCouponStartTime')); ?>
                                        </div>
                                        <?php echo $this->formElementErrors($form->get('getCouponStartTime'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('结束时间'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <?php echo $this->formElement($form->get('getCouponEndTime')); ?>
                                        </div>
                                        <?php echo $this->formElementErrors($form->get('getCouponEndTime'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('获取方式'); ?></label>
                                    <div class="col-sm-3">
                                        <?php echo $this->formElement($form->get('getCouponType')); ?>
                                        <?php echo $this->formElementErrors($form->get('getCouponType'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group shopping-amount" <?php if(isset($this->couponInfo) && $this->couponInfo->getGetCouponType() != 'buy') echo 'style="display: none;"'; ?>>
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('购物满'); ?></label>
                                    <div class="col-sm-3">
                                        <div class="input-group">
                                            <div class="input-group-addon"><?php echo $this->currencySymbol; ?></div>
                                            <?php echo $this->formElement($form->get('getShoppingAmount')); ?>
                                        </div>

                                        <?php echo $this->formElementErrors($form->get('getShoppingAmount'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group shopping-amount" <?php if(isset($this->couponInfo) && $this->couponInfo->getGetCouponType() != 'buy') echo 'style="display: none;"'; ?>>
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('每人可获取次数'); ?></label>
                                    <div class="col-sm-2">
                                        <?php echo $this->formElement($form->get('userGetFrequency')); ?>
                                        <?php echo $this->formElementErrors($form->get('userGetFrequency'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('指定客户组'); ?></label>
                                    <div class="col-sm-3">
                                        <?php echo $this->formElement($form->get('getUserType')); ?>
                                        <?php echo $this->formElementErrors($form->get('getUserType'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group user-group" <?php if(!isset($this->couponInfo) || $this->couponInfo->getGetUserType() != 'userGroup') echo 'style="display: none;"'; ?>>
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('客户分组'); ?></label>
                                    <div class="col-sm-8">
                                        <?php echo $this->formElement($form->get('userGroup')); ?>
                                        <?php echo $this->formElementErrors($form->get('userGroup'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('指定商品类型'); ?></label>
                                    <div class="col-sm-3">
                                        <?php echo $this->formElement($form->get('getGoodsType')); ?>
                                        <?php echo $this->formElementErrors($form->get('getGoodsType'), ['class'=>'error-message']); ?>
                                    </div>
                                </div>
                                <div class="form-group goods-class" <?php if(!isset($this->couponInfo) || $this->couponInfo->getGetGoodsType() != 'goodsClass') echo 'style="display: none;"'; ?>>
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('商品分类'); ?></label>
                                    <div class="col-sm-6">
                                        <table class="table table-hover">
                                            <tbody>
                                            <?php if($this->classList) {
                                                if(isset($this->couponInfo) && $this->couponInfo->getGetGoodsType() == 'goodsClass') $selectedClass = unserialize($this->couponInfo->getGetGoodsBody());
                                                foreach ($this->classList as $classValue) { ?>
                                                    <tr>
                                                        <td>
                                                            <label class="checkbox-inline">
                                                                <?php if($classValue->getClassTopId() > 0) echo str_repeat('&nbsp;', substr_count($classValue->getClassPath(), ',') * 3); ?>
                                                                <input onclick="selectClassId('<?php echo $classValue->getClassPath(); ?>', <?php echo $classValue->getClassId(); ?>);" name="goodsClass[]" <?php if(isset($selectedClass) && !empty($selectedClass) && in_array($classValue->getClassId(), $selectedClass)) echo 'checked'; ?> id="classId<?php echo $classValue->getClassId(); ?>" value="<?php echo $classValue->getClassId(); ?>" type="checkbox" class="minimal">
                                                                <?php echo $this->escapeHtml($classValue->getClassName() . ' (ID：' . $classValue->getClassId() . ')'); ?>
                                                            </label>
                                                        </td>
                                                    </tr>
                                                <?php } } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <div class="form-group goods-brand" <?php if(!isset($this->couponInfo) || $this->couponInfo->getGetGoodsType() != 'goodsBrand') echo 'style="display: none;"'; ?>>
                                    <label class="col-sm-2 control-label"><?php echo $this->translate('商品品牌'); ?></label>
                                    <div class="col-sm-6">
                                        <table class="table table-hover">
                                            <tbody>
                                            <?php if($this->brandList) {
                                                if(isset($this->couponInfo) && $this->couponInfo->getGetGoodsType() == 'goodsBrand') $selectedBrand = unserialize($this->couponInfo->getGetGoodsBody());
                                                foreach ($this->brandList as $brandId => $brandName) { ?>
                                                    <tr>
                                                        <td>
                                                            <label class="checkbox-inline">
                                                                <input name="goodsBrand[]" <?php if(isset($selectedBrand) && !empty($selectedBrand) && in_array($brandId, $selectedBrand)) echo 'checked'; ?> value="<?php echo $brandId; ?>" type="checkbox" class="minimal">
                                                                <?php echo $this->escapeHtml($brandName); ?>
                                                            </label>
                                                        </td>
                                                    </tr>
                                                <?php } } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>
</div>
<script type="text/javascript">
    laydate.render({elem: '#couponStartTime'});
    laydate.render({elem: '#couponEndTime'});
    laydate.render({elem: '#getCouponStartTime'});
    laydate.render({elem: '#getCouponEndTime'});

    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate({
            rules: {
                couponTitle: {
                    required: true
                },
                couponInfo: {
                    required: true
                }
            },
            messages: {
                couponTitle: {
                    required: "<?php echo $this->translate('优惠券名称不能为空!'); ?>"
                },
                couponInfo: {
                    required: "<?php echo $this->translate('优惠券描述不能为空!'); ?>"
                }
            }
        });
    });

    $('#getCouponType').change(function () {
        if(this.value == 'buy') {
            $('.shopping-amount').show();
        } else {
            $('.shopping-amount').hide();
        }
    });

    $('#getUserType').change(function () {
        if(this.value == 'allUser') {
            $('.user-group').hide();
        } else {
            $('.user-group').show();
        }
    });

    $('#getGoodsType').change(function () {
        if(this.value == 'allGoods') {
            $('.goods-class').hide();
            $('.goods-brand').hide();
        } else if (this.value == 'goodsBrand') {
            $('.goods-class').hide();
            $('.goods-brand').show();
        } else {
            $('.goods-brand').hide();
            $('.goods-class').show();
        }
    });
</script>